import request from '../../services/request';
const app = getApp();
const wordbookService = require('../../services/wordbook')

Page({
  data: {
    userInfo: null,
    isLogin: false,
    progress: 0,
    learned: 0,
    mastered: 0,
    selectedTab: 'unit', // 'unit' or 'plan'
    wordbooks: [],
    currentBook: null
  },

  onLoad() {
    // 只做页面初始化，不做登录判断
    this.loadWordbooks();
    // this.loadCurrentLearningBook();
  },

  async loadCurrentLearningBook() {
    const res = await request.get('/user/currentLearningBook');
    if(res.code === 200) {
      app.setCurrentLearningBook(res.data);
      this.setData({
        currentBook: res.data
      });
    }
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    // 保证每次进入都刷新当前学习书籍
    this.loadCurrentLearningBook();
  },

  // 加载词库列表
  async loadWordbooks() {
    const wordbooks = wordbookService.getWordbooks()
    
    // 获取每个词库的进度
    for (let book of wordbooks) {
      book.progress = await wordbookService.getProgress(book.id)
    }
    
    this.setData({
      wordbooks,
      currentBook: wordbooks[0]
    })

    // 更新学习进度
    if (wordbooks[0]) {
      this.setData({
        progress: wordbooks[0].progress,
        learned: Math.floor(wordbooks[0].totalWords * wordbooks[0].progress / 100),
        mastered: Math.floor(wordbooks[0].totalWords * wordbooks[0].progress / 200)
      })
    }
  },

  // 切换词库
  async switchBook(e) {
    const bookId = e.currentTarget.dataset.id
    const book = this.data.wordbooks.find(b => b.id === bookId)
    if (book) {
      this.setData({
        currentBook: book,
        progress: book.progress,
        learned: Math.floor(book.totalWords * book.progress / 100),
        mastered: Math.floor(book.totalWords * book.progress / 200)
      })
    }
  },

  // 分享给好友
  onShareAppMessage() {
    return {
      title: 'BigMouse记单词 - 轻松记住单词',
      path: '/pages/index/index',
      imageUrl: '/images/share-cover.png'
    }
  },

  // 搜索单词
  onSearch(e) {
    const keyword = e.detail.value
    if (keyword) {
      // TODO: 实现搜索功能
      console.log('搜索:', keyword)
    }
  },

  // 开始学习单词
  startLearning() {
    if (!this.data.currentBook) {
      wx.showToast({
        title: '请先选择词库',
        icon: 'none'
      })
      return
    }
    console.log('开始学习单词', this.data.currentBook)
    wx.navigateTo({
      url: `/pages/word/word?bookId=${this.data.currentBook.id}`
    })
  },

  // 随身听
  handleListen() {
    if (!this.data.isLogin) {
      this.goLogin()
      return
    }
    console.log('随身听')
  },

  // 报听写
  handleDictation() {
    if (!this.data.isLogin) {
      this.goLogin()
      return
    }
    console.log('报听写')
  },

  // 英文选义
  handleEnChoice() {
    if (!this.data.isLogin) {
      this.goLogin()
      return
    }
    console.log('英文选义')
  },

  // 中文选词
  handleCnChoice() {
    if (!this.data.isLogin) {
      this.goLogin()
      return
    }
    console.log('中文选词')
  },

  // 听音选义
  handleAudioChoice() {
    if (!this.data.isLogin) {
      this.goLogin()
      return
    }
    console.log('听音选义')
  },

  handleVIP() {
    if (!this.data.isLogin) {
      this.goLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/vip/vip'
    })
  },

  goLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  onTabChange(e) {
    this.setData({
      selectedTab: e.detail.value
    })
  },

  // 选择词书
  selectBook() {
    wx.navigateTo({
      url: '/pages/wordbook/wordbook'
    });
  },

  // 跳转到单元学习页面
  goToWordLearn() {
    if (!this.data.currentBook) {
      wx.showToast({
        title: '请先选择词书',
        icon: 'none'
      })
      return
    }

    wx.navigateTo({
      url: `/pages/wordlearn/wordlearn?bookId=${this.data.currentBook.bookId}`
    })
  },
}) 